package com.springboot.demo.service.impl;


import com.cx.item.common.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.mapper.GoodsMapper;
import com.springboot.demo.model.enums.GoodsTypeEnum;
import com.springboot.demo.model.example.GoodsExample;
import com.springboot.demo.model.model.Goods;
import com.springboot.demo.model.query.GoodsListQuery;
import com.springboot.demo.model.vo.GoodsDetailsVo;
import com.springboot.demo.model.vo.GoodsListVo;
import com.springboot.demo.service.GoodsBGService;
import com.springboot.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hwm on 2018/6/21.
 */
@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods, GoodsExample, GoodsMapper> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsBGService goodsAdminService;

    @Override
    public List<Goods> selectByExampleWithBLOBs(GoodsExample example) {
        List<Goods> goodsList = goodsMapper.selectByExampleWithBLOBs(example);
        return goodsList;
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Goods record) {
        return goodsMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public GoodsDetailsVo getGoodsDetails(Integer goodsId) {

        GoodsDetailsVo goodsDetails = goodsAdminService.getGoodsDetails(goodsId);

        return goodsDetails;
    }

    @Override
    public PageInfo<List<GoodsListVo>> getGoodsList(GoodsListQuery goodsListQuery) {

        PageHelper.startPage(goodsListQuery.getPageNum(), goodsListQuery.getPageSize());
        List<GoodsListVo> goodsListByClient = goodsMapper.getGoodsListByClient(goodsListQuery);
        goodsListByClient.stream().forEach(goodsListVo -> {
            goodsListVo.setUpdateTime(null);
            goodsListVo.setGoodsTypeName(GoodsTypeEnum.getByValue(goodsListVo.getGoodsTypeCode()).getName());
        });

        return new PageInfo(goodsListByClient);
    }

}
